package com.lh0811.forge.pserv.basic.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lh0811.forge.dependency.basic_code.BeanHelper;
import com.lh0811.forge.dependency.basic_code.IdWorker;
import com.lh0811.forge.dependency.basic_web.exception.CommonException;
import com.lh0811.forge.dependency.basic_web.response.ServerResponse;
import com.lh0811.forge.pserv.basic.api.param.CommBizNoCreateParam;
import com.lh0811.forge.pserv.basic.api.param.CommBizNoUpdateParam;
import com.lh0811.forge.pserv.basic.api.param.CommonBizNoQueryListParam;
import com.lh0811.forge.pserv.basic.api.param.CommonBizNoQueryPageParam;
import com.lh0811.forge.pserv.basic.api.vo.CommBizNoVo;
import com.lh0811.forge.pserv.basic.server.repository.dao.CommBizNoDao;
import com.lh0811.forge.pserv.basic.server.repository.entity.CommBizNo;
import com.lh0811.forge.pserv.basic.server.service.BizNoService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BizNoServiceImpl implements BizNoService {

    @Resource
    private CommBizNoDao commBizNoDao;

    /**
     * 查询业务编号列表(分页)
     */
    @Override
    public PageInfo<CommBizNoVo> queryBizNoPageList(CommonBizNoQueryPageParam param) throws Exception {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<CommBizNo> list = commBizNoDao.lambdaQuery()
                .eq(StringUtils.isNotBlank(param.getBizType()), CommBizNo::getBizType, param.getBizType())
                .like(StringUtils.isNotBlank(param.getBizTypeName()), CommBizNo::getBizTypeName, param.getBizTypeName())
                .like(StringUtils.isNotBlank(param.getPrefix()), CommBizNo::getPrefix, param.getPrefix())
                .list();
        PageInfo pageInfo = new PageInfo(list);
        PageHelper.clearPage();
        pageInfo.setList(BeanHelper.copyList(pageInfo.getList(), CommBizNoVo.class));
        return pageInfo;
    }

    /**
     * 查询业务编号列表
     */
    @Override
    public List<CommBizNoVo> queryBizNoList(CommonBizNoQueryListParam param) throws Exception {
        List<CommBizNo> list = commBizNoDao.lambdaQuery()
                .eq(StringUtils.isNotBlank(param.getBizType()), CommBizNo::getBizType, param.getBizType())
                .like(StringUtils.isNotBlank(param.getBizTypeName()), CommBizNo::getBizTypeName, param.getBizTypeName())
                .like(StringUtils.isNotBlank(param.getPrefix()), CommBizNo::getPrefix, param.getPrefix())
                .list();
        PageInfo pageInfo = new PageInfo(list);
        return BeanHelper.copyList(pageInfo.getList(), CommBizNoVo.class);
    }

    /**
     * 创建业务编号记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createBizNo(CommBizNoCreateParam param) throws Exception {
        // 判断业务编号是否存在
        CommBizNo commBizNo = commBizNoDao.lambdaQuery()
                .eq(CommBizNo::getBizType, param.getBizType())
                .one();
        if (ObjectUtils.isEmpty(commBizNo)) {
            throw CommonException.create(ServerResponse.createByError("业务类型已存在"));
        }
        CommBizNo newCommBizNo = new CommBizNo();
        newCommBizNo.setId(IdWorker.nextId()); // 主键
        newCommBizNo.setBizType(param.getBizType()); // 业务类型
        newCommBizNo.setBizTypeName(param.getBizTypeName()); // 业务类型
        newCommBizNo.setStep(param.getStep()); // 每次获取多少个序号
        newCommBizNo.setPrefix(param.getPrefix()); // 前缀
        newCommBizNo.setBatch(param.getBatch()); // 批次
        newCommBizNo.setBatchLen(param.getBatchLen()); // 批次长度
        newCommBizNo.setCurrentNum(param.getCurrentNum()); // 当前序号
        newCommBizNo.setNumLen(param.getNumLen()); // 序号长度
        newCommBizNo.setFillChar(param.getFillChar()); // 批次或序号长度不足时填充符号
        commBizNoDao.save(newCommBizNo);
    }

    /**
     * 修改业务编号记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editBizNo(CommBizNoUpdateParam param) throws Exception {
        CommBizNo commBizNo = commBizNoDao.getById(param.getId());
        if (ObjectUtils.isEmpty(commBizNo)) {
            throw CommonException.create(ServerResponse.createByError("业务编号记录不存在"));
        }
        CommBizNo updateCommBizNo = BeanHelper.copyBean(param, CommBizNo.class);
        commBizNoDao.updateById(updateCommBizNo);
    }

    /**
     * 删除业务编号记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBizNo(Long bizNoId) throws Exception {
        CommBizNo commBizNo = commBizNoDao.getById(bizNoId);
        if (ObjectUtils.isEmpty(commBizNo)) {
            throw CommonException.create(ServerResponse.createByError("业务编号记录不存在"));
        }
        commBizNoDao.removeById(bizNoId);
    }

}
